package ch.bfh.mgb.hamilton.simulation;

import java.util.HashSet;
import java.util.Set;

import ch.bfh.mgb.hamilton.entities.Food;
import ch.bfh.mgb.hamilton.entities.Robot;

/**
 * Class that stores and manages the food entities.
 * Kind of a container that'll be used for every generation.
 */
public class FoodContainer {
	private int smallElementCount;
	private int mediumElementCount;
	private int bigElementCount;
	
	public static final Food SMALL = new Food(1, 1);
	public static final Food MEDIUM = new Food(3, 2);
	public static final Food BIG = new Food(6, 3);
	
	private Set<Robot> waitForMedium;
	private Set<Robot> waitForBig;
	
	public FoodContainer() {
		initContainer();
	}
	
	public void initContainer() {
		smallElementCount = SimulationParameters.initialSmallFood;
		mediumElementCount = SimulationParameters.initialMediumFood;
		bigElementCount = SimulationParameters.initialBigFood;
		waitForMedium = new HashSet<Robot>();
		waitForBig = new HashSet<Robot>();
	}
	
	public void cleanWaiters() {
		waitForMedium.clear();
		waitForBig.clear();
	}

	public void collectSmallElement(Robot robot) {
		smallElementCount--;
		if (smallElementCount > 0) {
			robot.setSmallFoodCollected(robot.getSmallFoodCollected() + 1);
		}
	}

	public void collectMediumElement(Robot robot) {
		waitForMedium.add(robot);
		if (mediumElementCount > 0) {
			if (waitForMedium.size() >= MEDIUM.weight) {
				mediumElementCount--;
				for(Robot r : waitForMedium) {
					r.setMediumFoodCollected(r.getMediumFoodCollected() + 1);
				}
				waitForMedium.clear();
			}
		}
	}

	public void collectBigElement(Robot robot) {
		waitForBig.add(robot);
		if (bigElementCount > 0) {
			if (waitForBig.size() >= BIG.weight) {
				bigElementCount--;
				for(Robot r : waitForBig) {
					r.setBigFoodCollected(r.getBigFoodCollected() + 1);
				}
				waitForBig.clear();
			}
		}
	}
}
